/*
 * Utiles_Fecha.java
 *
 * Created on 17 de julio de 2007, 04:30 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package helper;
import java.util.*;
/**
 *
 * 
 */
public class Utiles_Fecha_Cliente {

    
    /** Creates a new instance of Utiles_Fecha */

    public Utiles_Fecha_Cliente( String d, String m, String a ) {
        dia = d;
        mes = m;
        anio = a;
    }
    
    public Utiles_Fecha_Cliente( Date f ){
        StringTokenizer str = new  StringTokenizer( f.toString() );
        this.nom_dia = str.nextToken();
        mes = identificaMes( str.nextToken() );
        dia = str.nextToken();
        str.nextToken();
        str.nextToken();
        anio = str.nextToken();
        
    }
    
    public Utiles_Fecha_Cliente( Date f, String d ){
        StringTokenizer str = new  StringTokenizer( f.toString(), d );
        str.nextToken();
        mes = identificaMes( str.nextToken() );
        dia = str.nextToken();
        str.nextToken();
        str.nextToken();
        anio = str.nextToken();        
    }
    
    public Utiles_Fecha_Cliente( String fec ) {
        StringTokenizer str = new  StringTokenizer( fec, "/" );        
        dia = str.nextToken();
        mes = str.nextToken();
        anio = str.nextToken();
    }


    public String getDia(){
        return dia;                
    }
    
    public String getMes(){
        return mes;        
    }
    
    public String getAnio(){
        return anio;
    }
    
    public String getFecha(){
        return dia + "/" + mes + "/" + anio;
    }
    
    public Date getDate(){
        int a = Integer.parseInt( anio );
        int m = Integer.parseInt( mes );
        int d = Integer.parseInt( dia );        
        return new Date( a - 1900, m - 1, d );
    }
    
    public boolean isValida(){
        int a = Integer.parseInt( anio );
        int m = Integer.parseInt( mes );        
        int d = Integer.parseInt( dia );
        
        if( d > 31 || d <= 0 ){        
            return false;
        }
        if( m > 12 || m <= 0 ){        
            return false;
        }
        return true;
    }
    
    public void setDia( String d ){
        dia = d;
    }
    
    public void setMes( String m ){
        mes = m;
    }
    
    public void setAnio( String a ){
        anio = a;
    }  
    
    //Funciones Estaticas    
    public static String[] proyectaMesFechas( String fec, int p ){
        String[] proy = new String[ p ];
        String fecTemp = fec;
        for( int i = 0; i < p; i++ ){
            fecTemp = siguienteMes( fecTemp ).getFecha();
            proy[i] = fecTemp;
        }
        return proy;
    } 
    
    public static Utiles_Fecha_Cliente[] proyectaMesFechas2( String fec, int p ){
        Utiles_Fecha_Cliente[] proy = new Utiles_Fecha_Cliente[p];
        String fecTemp = fec;
        Utiles_Fecha_Cliente temp = null;
        for( int i = 0; i < p; i++ ){
            temp = siguienteMes( fecTemp );
            proy[i] = temp;
            fecTemp = temp.getFecha();
        }
        return proy;
    }     

    public String getNombreMes(String mes){
        if( mes.equals( "01" ) ){
            return "Enero";            
        }else if( mes.equals( "02" ) ){
            return "Febrero";
        }else if( mes.equals( "03" ) ){
            return "Marzo";
        }else if( mes.equals( "04" ) ){
            return "Abril";            
        }else if( mes.equals( "05" ) ){
            return "Mayo";
        }else if( mes.equals( "06" ) ){
            return "Junio";
        }else if( mes.equals( "07" ) ){
            return "Julio";
        }else if( mes.equals( "08" ) ){
            return "Agosto";
        }else if( mes.equals( "09" ) ){
            return "Septiembre";
        }else if( mes.equals( "10" ) ){
            return "Octubre";
        }else if( mes.equals( "11" ) ){
            return "Noviembre";
        }else if( mes.equals( "12" ) ){
            return "Diciembre";
        }
        return null;        
    }

    public static String identificaMes( String mes ){
        if( mes.equals( "Jan" ) ){
            return "01";            
        }else if( mes.equals( "Feb" ) ){
            return "02";
        }else if( mes.equals( "Mar" ) ){
            return "03";
        }else if( mes.equals( "Apr" ) ){
            return "04";            
        }else if( mes.equals( "May" ) ){
            return "05";
        }else if( mes.equals( "Jun" ) ){
            return "06";
        }else if( mes.equals( "Jul" ) ){
            return "07";
        }else if( mes.equals( "Aug" ) ){
            return "08";
        }else if( mes.equals( "Sep" ) ){
            return "09";
        }else if( mes.equals( "Oct" ) ){
            return "10";
        }else if( mes.equals( "Nov" ) ){
            return "11";
        }else if( mes.equals( "Dec" ) ){
            return "12";
        }
        return null;        
    }
    
    public static Utiles_Fecha_Cliente siguienteMes( String fec ){
                
        Utiles_Fecha_Cliente utemp = new Utiles_Fecha_Cliente( fec );
        
        int val = getSigMes( Integer.parseInt( utemp.getMes() ) );
        if( val != -1 ){            
            String m = null;
            if( val != 0 ){
                m = "" + val;                
            }else{
                utemp.setAnio( "" + getSigAnio( Integer.parseInt( utemp.getAnio() ) ) );
                m = "1";
            }            
            utemp.setMes( m );            
        }                
        return utemp;        
    }
    
    static int getSigMes( int m ){
        if( m > 0 && m < 12 ){
            return m + 1;
        }else if ( m == 12 ){
            return 0;
        }
        return -1;
    }
    public String getNomDia(){
        return identificaDia();
    }   
    public String identificaDia(){
        if(this.nom_dia.equalsIgnoreCase("Mon")){
            this.nom_dia = "Lunes";
        }else if(this.nom_dia.equalsIgnoreCase("Tue")){
            this.nom_dia = "Martes";
        }else  if(this.nom_dia.equalsIgnoreCase("Wed")){
            this.nom_dia = "Miercoles";
        }else if(this.nom_dia.equalsIgnoreCase("Thu")){
            this.nom_dia = "Jueves";
        }else if(this.nom_dia.equalsIgnoreCase("Fri")){
            this.nom_dia = "Viernes";
        }else  if(this.nom_dia.equalsIgnoreCase("Sat")){
            this.nom_dia = "Sabado";
        }else if(this.nom_dia.equalsIgnoreCase("Sun")){
            this.nom_dia = "Domingo";
        }
        return nom_dia;
    }
    
    static int getSigAnio( int a ){
        return a + 1;
    }

    public static int calculaDias(int a, int m)throws Exception{
        if ( (m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8 ) || (m == 10) || (m == 12) ){
            return 31;
        }
        else if ( (m == 4) || (m == 6) || (m == 9) || (m == 11) ) {
            return 30;
        }else if (m == 2){
            if ( ( ( ( a % 4 ) == 0 ) && ( ( a % 100 ) ) != 100 ) || ( ( a % 400 ) == 0 ) ) {
                return 29;
            }
            return 28;
        }else{
            return -1;
        }
    }
        
    private String dia;
    private String mes;
    private String anio;    
    private String nom_dia;        
    private String nom_mes;
    
}
